/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 08/03/2011, 09:06:07
 */

package sisgraf.view;
import com.itextpdf.text.DocumentException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import sisgraf.controler.FaturamentoPeriodoControler;
import sisgraf.controler.FaturamentoPeriodoTabela;
import sisgraf.impressao.ImprimePdf;

/**
 *
 * @author jordan C. Guerra
 */
public class FaturamentoPeriodo extends javax.swing.JDialog {
    private FaturamentoPeriodoControler modeloControler =new FaturamentoPeriodoControler();
    private String condicao;
    private FaturamentoPeriodoTabela modelo;

    
    /** Creates new form Principal */
    public FaturamentoPeriodo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        formComponentResized(null);
        setLocationRelativeTo(null);
        jTbVisualizacao.setModel(modeloControler.getListarPedido(null));
        this.setVisible(true);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPnlSuperior = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPnlCentral = new javax.swing.JPanel();
        jPnlConteudo = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jBtConsultar = new javax.swing.JButton();
        jBtImprimir = new javax.swing.JButton();
        jBtFinalizar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTbVisualizacao = new javax.swing.JTable();
        jTxFdDatIni = new javax.swing.JFormattedTextField();
        jTxFdDatFim = new javax.swing.JFormattedTextField();
        jPnlControle = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jTxFdTotal = new javax.swing.JFormattedTextField();
        jPnlInferior = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Faturamento da Gráfica por Período");
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jPnlSuperior.setBorder(new javax.swing.border.MatteBorder(null));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("SISTEMA DE ORÇAMENTOS PARA SERVIÇOS GRAFICOS");
        jLabel1.setMinimumSize(new java.awt.Dimension(0, 14));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Faturamento da Gráfica por Período");
        jLabel2.setMinimumSize(new java.awt.Dimension(0, 14));

        javax.swing.GroupLayout jPnlSuperiorLayout = new javax.swing.GroupLayout(jPnlSuperior);
        jPnlSuperior.setLayout(jPnlSuperiorLayout);
        jPnlSuperiorLayout.setHorizontalGroup(
            jPnlSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 689, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 689, Short.MAX_VALUE)
        );
        jPnlSuperiorLayout.setVerticalGroup(
            jPnlSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlSuperiorLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPnlCentral.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPnlCentral.setMinimumSize(new java.awt.Dimension(100, 0));

        jPnlConteudo.setBorder(javax.swing.BorderFactory.createLineBorder(null));

        jLabel5.setText("Período de");

        jLabel6.setText("até");

        jBtConsultar.setText("Consultar");
        jBtConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtConsultarActionPerformed(evt);
            }
        });

        jBtImprimir.setText("Imprimir");
        jBtImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtImprimirActionPerformed(evt);
            }
        });

        jBtFinalizar.setText("Finalizar");
        jBtFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtFinalizarActionPerformed(evt);
            }
        });

        jTbVisualizacao.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jTbVisualizacao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTbVisualizacao);

        try {
            jTxFdDatIni.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jTxFdDatFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jPnlConteudoLayout = new javax.swing.GroupLayout(jPnlConteudo);
        jPnlConteudo.setLayout(jPnlConteudoLayout);
        jPnlConteudoLayout.setHorizontalGroup(
            jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlConteudoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTxFdDatIni, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTxFdDatFim, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 196, Short.MAX_VALUE)
                .addComponent(jBtConsultar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBtImprimir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBtFinalizar)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 685, Short.MAX_VALUE)
        );
        jPnlConteudoLayout.setVerticalGroup(
            jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlConteudoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlConteudoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jBtFinalizar)
                    .addComponent(jBtImprimir)
                    .addComponent(jBtConsultar)
                    .addComponent(jTxFdDatIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTxFdDatFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))
        );

        jPnlControle.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel7.setText("Total do Período:");

        jTxFdTotal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.##"))));
        jTxFdTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jTxFdTotal.setEnabled(false);
        jTxFdTotal.setFont(new java.awt.Font("Tahoma", 1, 11));

        javax.swing.GroupLayout jPnlControleLayout = new javax.swing.GroupLayout(jPnlControle);
        jPnlControle.setLayout(jPnlControleLayout);
        jPnlControleLayout.setHorizontalGroup(
            jPnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlControleLayout.createSequentialGroup()
                .addContainerGap(452, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTxFdTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPnlControleLayout.setVerticalGroup(
            jPnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlControleLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPnlControleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTxFdTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPnlCentralLayout = new javax.swing.GroupLayout(jPnlCentral);
        jPnlCentral.setLayout(jPnlCentralLayout);
        jPnlCentralLayout.setHorizontalGroup(
            jPnlCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPnlConteudo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPnlControle, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPnlCentralLayout.setVerticalGroup(
            jPnlCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlCentralLayout.createSequentialGroup()
                .addComponent(jPnlConteudo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlControle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPnlInferior.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Gráfica Modêlo");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Grupo de Estudo PMA Anhanguera");

        javax.swing.GroupLayout jPnlInferiorLayout = new javax.swing.GroupLayout(jPnlInferior);
        jPnlInferior.setLayout(jPnlInferiorLayout);
        jPnlInferiorLayout.setHorizontalGroup(
            jPnlInferiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlInferiorLayout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 371, Short.MAX_VALUE)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPnlInferiorLayout.setVerticalGroup(
            jPnlInferiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlInferiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPnlSuperior, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPnlInferior, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPnlCentral, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPnlSuperior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlCentral, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPnlInferior, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        // TODO add your handling code here:
        int dif = this.getHeight()-jPnlSuperior.getHeight()-jPnlCentral.getHeight()-jPnlInferior.getHeight()-6;
        jPnlCentral.setSize(this.getWidth(),this.getHeight()-jPnlSuperior.getHeight()-jPnlInferior.getHeight()-dif);
        jPnlConteudo.setSize(jPnlCentral.getWidth(),jPnlCentral.getHeight()-jPnlControle.getHeight());
        jPnlControle.setSize(jPnlCentral.getWidth(), jPnlControle.getHeight());
        jPnlControle.setLocation(0, jPnlConteudo.getHeight());
    }//GEN-LAST:event_formComponentResized

    private void jBtConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtConsultarActionPerformed
        // TODO add your handling code here:
        condicao = "";
        String dataIni = "";
        String dataFim = "";
        String dataPes = "";
        String erroMensagem = ""; // jTxFdNumPed
        if (!Menu.validaData(jTxFdDatIni.getText())){
            erroMensagem=erroMensagem+"Data inicial invalida\n";
        } else {
            dataIni=jTxFdDatIni.getText();
        }
        if (!Menu.validaData(jTxFdDatFim.getText())){
            erroMensagem=erroMensagem+"Data final invalida\n";
        } else {
            dataFim=jTxFdDatFim.getText();
        }
        dataPes="p.dataFaturamento";
        condicao = dataPes+ " >= '" +Menu.convData(dataIni, 0) + "' and "+dataPes+" <= '" +Menu.convData(dataFim, 0)+"'";
        if (!erroMensagem.equals("")){
            erroMensagem="Foram encontrados os seguintes erros no filtro de pesquisa:\n"+erroMensagem;
           javax.swing.JOptionPane.showMessageDialog(null, erroMensagem , "Dados Informados Invalidos", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        } else {
            jTbVisualizacao.setModel(modeloControler.getListarPedido(condicao));
            modeloControler.ajustarTabela(jTbVisualizacao);
            modelo = (FaturamentoPeriodoTabela) jTbVisualizacao.getModel();
            int nLinhas=modelo.getRowCount();
            BigDecimal total=new BigDecimal("0");
            for (int i=0;i<nLinhas;i++){
                total=total.add(modelo.getPedido(i).getValor());
            }
            jTxFdTotal.setText(total.toString());
        }

    }//GEN-LAST:event_jBtConsultarActionPerformed

    private void jBtFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtFinalizarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jBtFinalizarActionPerformed

    private void jBtImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtImprimirActionPerformed
        // TODO add your handling code here:
                 if(jTbVisualizacao.getRowCount()==0)
         JOptionPane.showMessageDialog(null,"Não há dados para impressão!");
     else
     {
        ArrayList lista = new ArrayList();
        int i=0,j=0;
        for(j=0;j<jTbVisualizacao.getColumnCount();j++)
            lista.add(jTbVisualizacao.getColumnName(j));
        for(i=0;i<jTbVisualizacao.getRowCount();i++)
            for(j=0;j<jTbVisualizacao.getColumnCount();j++)
            lista.add(jTbVisualizacao.getValueAt(i,j));
    ImprimePdf impr = new ImprimePdf();
            try {
                impr.imprimir(lista, jTbVisualizacao.getColumnCount(), "Faturamento de " + jTxFdDatIni.getText() + " a " + jTxFdDatFim.getText(), jTxFdTotal.getText(), true,false,null,null);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FaturamentoPeriodo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (DocumentException ex) {
                Logger.getLogger(FaturamentoPeriodo.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(FaturamentoPeriodo.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }//GEN-LAST:event_jBtImprimirActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Principal dialog = new Principal(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtConsultar;
    private javax.swing.JButton jBtFinalizar;
    private javax.swing.JButton jBtImprimir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPnlCentral;
    private javax.swing.JPanel jPnlConteudo;
    private javax.swing.JPanel jPnlControle;
    private javax.swing.JPanel jPnlInferior;
    private javax.swing.JPanel jPnlSuperior;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTbVisualizacao;
    private javax.swing.JFormattedTextField jTxFdDatFim;
    private javax.swing.JFormattedTextField jTxFdDatIni;
    private javax.swing.JFormattedTextField jTxFdTotal;
    // End of variables declaration//GEN-END:variables
    
}
